package com.car.order.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.car.commons.core.model.Result;
import com.car.commons.core.model.WoniuException;
import com.car.commons.web.util.StateUtil;
import com.car.order.client.CarShoppingClient;
import com.car.order.dto.alipay.PayInfoDto;
import com.car.order.entity.ServeOrderLog;
import com.car.order.entity.ServiceOrderInfo;
import com.car.order.mapper.ServiceOrderInfoMapper;
import com.car.order.service.ServeOrderLogService;
import com.car.order.util.AlipayUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@RabbitListener(queues = "serve_order_check_queue")
public class CheckServeOrderConsumer {
    @Autowired
    private AlipayUtil alipayUtil;
    @Autowired
    private ServiceOrderInfoMapper serviceOrderInfoMapper;
    @Resource
    private ServeOrderLogService serveOrderLogService;



    @RabbitHandler
    public void handler(String orderNum) throws Exception {
        PayInfoDto payInfoDto = alipayUtil.queryPayState(orderNum);
        System.out.println(payInfoDto);
        ServiceOrderInfo orderInfo = serviceOrderInfoMapper.selectById(orderNum);
        if (payInfoDto.getPayState().equals("TRADE_SUCCESS")) {
            if (orderInfo.getOrderState().equals(StateUtil.SERVE_ORDER_STATE_UNPAID)) {
                //修改订单状态
                orderInfo.setOrderState(StateUtil.SERVE_ORDER_STATE_NOT_SERVED);
                //支付时间
                LocalDateTime localDateTime = LocalDateTime.parse(payInfoDto.getPayDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                orderInfo.setOrderPayTime(localDateTime.toString());
                serviceOrderInfoMapper.updateById(orderInfo);
                ServeOrderLog serveOrderLog = new ServeOrderLog();
                serveOrderLog.setServeOrderId(orderNum);
                serveOrderLog.setLogDesc("服务订单支付完成");
                serveOrderLog.setLogTime(localDateTime.toString());
                serveOrderLogService.save(serveOrderLog);
            }
        }else {
            if (orderInfo.getOrderState().equals(StateUtil.SERVE_ORDER_STATE_UNPAID)) {
                orderInfo.setOrderState(StateUtil.SERVE_ORDER_STATE_CANCEL);
                ServeOrderLog serveOrderLog = new ServeOrderLog();
                serveOrderLog.setServeOrderId(orderNum);
                serveOrderLog.setLogDesc("支付超时，自动取消订单");
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                serveOrderLog.setLogTime(LocalDateTime.now().format(formatter));
                serveOrderLogService.save(serveOrderLog);
                serviceOrderInfoMapper.updateById(orderInfo);
            }
        }
    }
}
